package JUnit;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import Servlets.WhatsAppConstatnts;
import protocol.Headers;

public class Queue extends mainTest{


	@Before
	public void setUp() throws Exception {
		_url = "queue";
	}


	@Test
	public void test405() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains("HTTP/1.1 " + Headers.STATUS_405));
	}

	@Test
	public void test403() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"GET /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains(Headers.STATUS_403));
	}
	
	@Test
	public void testSendDirect() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /send.jsp HTTP/1.1\nCookie: "
								+ cookie2
								+ "\n\nType=Direct&Target=0546861622&Content=gggggggggg\n$"));
		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"POST /send.jsp HTTP/1.1\nCookie: "
						+ cookie2
						+ "\n\nType=Direct&Target=0546861622&Content=ggggggggg2g\n$"));
		String res = 		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"GET /"
						+ _url
						+ ".jsp HTTP/1.1\nCookie: "
						+ cookie
						+ "\n\n$"));
		assertTrue(res.contains("From: yochailehman2\nMessage: gggggggggg\nFrom: yochailehman2\nMessage: ggggggggg2g"));
		
	}

	@Test
	public void testSendGroup() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);

		String response6 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd2&Phone=1234562\n$"));
		String cookie3 = this.getCookie(response6);

		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,userToAdd2\n$"));

		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /send.jsp HTTP/1.1\nCookie: "
								+ cookie2
								+ "\n\nType=Group&Target=TestGroup1&Content=qqqqqqqqq\n$"));
		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"POST /send.jsp HTTP/1.1\nCookie: "
						+ cookie2
						+ "\n\nType=Group&Target=TestGroup1&Content=qqqqq3qqqq\n$"));
		String res = 		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"GET /"
						+ _url
						+ ".jsp HTTP/1.1\nCookie: "
						+ cookie3
						+ "\n\n$"));
		assertTrue(res.contains("From: TestGroup1\nMessage: qqqqqqqqq\nFrom: TestGroup1\nMessage: qqqqq3qqqq"));
	}
	
	@Test
	public void testSendGroupAndDirect() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String cookie2 = this.getCookie(response5);

		String response6 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd2&Phone=1234562\n$"));
		String cookie3 = this.getCookie(response6);

		this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,userToAdd2\n$"));

		String response1 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie = this.getCookie(response1);
		this.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /send.jsp HTTP/1.1\nCookie: "
								+ cookie2
								+ "\n\nType=Group&Target=TestGroup1&Content=qqqqqqqqq\n$"));
		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"POST /send.jsp HTTP/1.1\nCookie: "
						+ cookie2
						+ "\n\nType=Direct&Target=1234562&Content=userMSG!\n$"));
		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"POST /send.jsp HTTP/1.1\nCookie: "
						+ cookie2
						+ "\n\nType=Group&Target=TestGroup1&Content=qqqqq3qqqq\n$"));
		String res = 		this.runtest(new RunnableClient(
				"localhost",
				9969,
				"GET /"
						+ _url
						+ ".jsp HTTP/1.1\nCookie: "
						+ cookie3
						+ "\n\n$"));
		assertTrue(res.contains("From: TestGroup1\nMessage: qqqqqqqqq\nFrom: userToAdd2\nMessage: userMSG!\nFrom: TestGroup1\nMessage: qqqqq3qqqq"));
	}

}
